﻿Imports System.Data.OleDb
Public Class 票据套打
    Public conn52 As New OleDbConnection(HAYJS.ConnStringOle)
    Public OleDbDataAdapter52 As OleDbDataAdapter = New OleDbDataAdapter("select * from 打印设置", conn52)
    Public DataSet52 As New DataSet
    Public BindingSource52 As New BindingSource
    Public OleDbCommandBuilder52 As New OleDbCommandBuilder(OleDbDataAdapter52)
    Public Pos52

    Public conn101 As New OleDbConnection(HAYJS.ConnStringOle)
    Public OleDbDataAdapter101 As OleDbDataAdapter = New OleDbDataAdapter("select * from 项目明细 order by 项目编号 ASC", conn101)
    Public DataSet101 As New DataSet
    Public BindingSource101 As New BindingSource
    Public OleDbCommandBuilder101 As New OleDbCommandBuilder(OleDbDataAdapter101)
    Public Pos101

    Private Sub 票据套打_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        OleDbDataAdapter52.Fill(DataSet52, "打印设置")
        BindingSource52.DataSource = DataSet52.Tables("打印设置")
        If HAYJS.ZhengBan Then
            BtAdd.Enabled = True
            BtDel.Enabled = True
        End If
        OleDbDataAdapter101.Fill(DataSet101, "项目明细")
        BindingSource101.DataSource = DataSet101.Tables("项目明细")
        Dgv101.DataSource = BindingSource101
        FormatDataGrid00(Dgv101)
        Dgv101.ReadOnly = False
        Dgv101.Columns("项目编号").ReadOnly = True
        Dgv101.Columns("项目编号").DefaultCellStyle.BackColor = Color.Ivory
        Dgv101.Columns("项目编号").Width = 60
        Dgv101.Columns("项目名称").Width = 180
        Dgv101.Columns("项目内容").Width = 360
        Dgv101.Columns("左边距").Width = 60
        Dgv101.Columns("上边距").Width = 60
        Dgv101.Columns("字体名称").Width = 120
        Dgv101.Columns("字体大小").Width = 60
    End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        Dim PosX, PosY As Double
        Dim BiLi As Double
        BiLi = BindingSource52(0)("比例尺")
        PosX = BindingSource52(0)("左右偏移")
        PosY = BindingSource52(0)("上下偏移")
        Dim MyFont As Font
        Dim MyStr As String
        Dim MyLeft, MyRight As Integer
        For i = 0 To Dgv101.RowCount - 1
            MyFont = New Font(Dgv101.Rows(i).Cells("字体名称").Value.ToString, CInt(Dgv101.Rows(i).Cells("字体大小").Value), FontStyle.Regular)
            MyStr = Dgv101.Rows(i).Cells("项目内容").Value.ToString
            MyLeft = CInt(Dgv101.Rows(i).Cells("左边距").Value * BiLi + PosX * BiLi)
            MyRight = CInt(Dgv101.Rows(i).Cells("上边距").Value * BiLi + PosY * BiLi)
            e.Graphics.DrawString(MyStr, MyFont, Brushes.Black, MyLeft, MyRight)
        Next

        '********正版验证开始
        'If HAYJS.ZhengBan = False Then
        '    e.Graphics.DrawString("缙绅软件工作室测试专用，注册请联系：13813795089 QQ：33715519！！！", New Font("黑体", 10, FontStyle.Regular), Brushes.Black, PosX + 20, PosY + 20)
        'End If
        '********正版验证结束
    End Sub

    Private Sub BtPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            PrintDocument1.Print()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub BtAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtAdd.Click
        BindingSource101.AddNew()
        If BindingSource101.Count = 1 Then
            BindingSource101(0)("项目编号") = "0001"
            BindingSource101(0)("字体名称") = "宋休"
            BindingSource101(0)("字体大小") = 10
            BindingSource101(0)("左边距") = 0
            BindingSource101(0)("上边距") = 0

        Else
            Pos101 = BindingSource101.Position
            BindingSource101(Pos101)("项目编号") = (Val(BindingSource101(Pos101 - 1)("项目编号").ToString.Trim) + 1).ToString.PadLeft(4, "0")
            BindingSource101(Pos101)("字体名称") = BindingSource101(Pos101 - 1)("字体名称")
            BindingSource101(Pos101)("字体大小") = BindingSource101(Pos101 - 1)("字体大小")
            BindingSource101(Pos101)("左边距") = 0
            BindingSource101(Pos101)("上边距") = 0
        End If
    End Sub

    Private Sub BtDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtDel.Click
        Try
            If BindingSource101.Count > 0 Then
                BindingSource101.RemoveCurrent()
            Else
                MsgBox("已经没有可删除的记录！！！")
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub BtSaveModel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            Me.Validate()
            BindingSource101.EndEdit()
            OleDbDataAdapter101.Update(DataSet101, "项目明细")
            MsgBox("模板保存成功！！！")
        Catch ex As Exception
            MsgBox("模板保存失败！！！")
        End Try
    End Sub

    Private Sub BtSaveData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtSaveData.Click
        Try
            Me.Validate()
            BindingSource101.EndEdit()
            OleDbDataAdapter101.Update(DataSet101, "项目明细")
            MsgBox("数据保存成功！！！")
        Catch ex As Exception
            MsgBox("数据保存失败！！！")
        End Try
    End Sub

    Private Sub BtEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            For i = 0 To BindingSource101.Count - 1
                BindingSource101(i)("项目编号") = (i + 1).ToString.PadLeft(4, "0")
            Next
            Me.Validate()
            BindingSource101.EndEdit()
            OleDbDataAdapter101.Update(DataSet101, "项目明细")
        Catch ex As Exception

        End Try
    End Sub

    Private Sub BtPreView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            PrintPreviewDialog1.Document = PrintDocument1
            PrintPreviewDialog1.ShowDialog()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub BtChoice_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            PageSetupDialog1.Document = PrintDocument1
            PageSetupDialog1.ShowDialog()
            PrintDocument1.PrinterSettings = PageSetupDialog1.PrinterSettings
        Catch ex As Exception

        End Try
    End Sub
End Class